package ui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.*;
import javax.swing.text.html.HTML.Tag;

//import system.Algorithm;

import common.Author;
import common.Book;
import database.*;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.SwingWorker;

import system.Algorithm;
import system.NmfAlg;

import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;


public class MainMenuUI extends JFrame{


		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		public static JTextField textField_1;
		public static JTextField textField;
		public static JTextField textField_2;
		private JList selectedBooks;
		public JComboBox comboBox;
		private JList booklist;
		public JFileChooser chooser;
		public File file;
		private DefaultListModel listModel;
		private ArrayList<Integer> authoridarry;
		private int flag;
		public static MainMenuUI X;  // Main window
		private ArrayList<Author> arrAuthors; // An array that contains authors.
		private ArrayList<Book> BookListArray=BookManager.getAllBooks(); // An array contains books to be shown in books list.
		private ArrayList<Book> SelectedBookListArray=new ArrayList<Book>(); // An array contains books that have been selected.
		public static ArrayList<Book> RunningBooks=new ArrayList<Book>(); // An array list contains all books that run at the moment.
		private String[] strauthorArr; // an array contains all names+last names of authors.
		DefaultListModel selectedBooksListModel= new DefaultListModel();
		public static JProgressBar progressBar;
		public static JLabel lblRunStatus;
		public static JButton button_2;
		public static ArrayList<String> pathes;
		private Thread AlgorithmThread;
		public static JTextField txtpr;
		public static int alg;
		public static JButton button_4;
		public static JButton btnResults;
		public static JButton button_3;
		private JMenu arg0_1;
		/**
		 * Constructor
		 * @throws IOException 
		 */
		public MainMenuUI() {
			
		       Image image = null;
			try {
				image = ImageIO.read(new File("images\\body.jpg"));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			setContentPane(new ImagePanel(image));
			
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
			getContentPane().setFont(new Font("MV Boli", Font.BOLD, 13));
			setSize(new Dimension(968, 507));
			setResizable(false);
			getContentPane().setLayout(null);
			
			
			ImageIcon b15 = new ImageIcon("images\\stop.png");
			
			////
			
			button_3 = new JButton(b15);
			button_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					AlgorithmThread.stop();
					lblRunStatus.setText("NMF PROCCESS STOPED!");
					button_2.setEnabled(true);
					button_4.setEnabled(true);
					button_3.setEnabled(false);
					progressBar.setValue(0);
					
					ChangeProgressBar();
					MainMenuUI.progressBar.setIndeterminate(false);
					
					btnResults.setEnabled(false);
					//ResultsUI.lblNumberOfGroups.setVisible(false);
					//ResultsUI.lblRandIndexValue.setVisible(false);
					
				}
			});
			button_3.setEnabled(false);
			
			chooser= new JFileChooser("books");
			
			ImageIcon b1 = new ImageIcon("images\\browse.png");
			JButton btnBrowse = new JButton(b1);
			btnBrowse.setBorder(BorderFactory.createEmptyBorder());
		
			btnBrowse.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {

						Scanner s=null;
						 int r = chooser.showOpenDialog(new JFrame());
						    if (r == JFileChooser.APPROVE_OPTION) {
						      String name = chooser.getSelectedFile().getAbsolutePath();
						      textField_1.setText(name);
						      file=chooser.getSelectedFile();}
				  }
				});

			btnBrowse.setBounds(211, 128, 89, 22);
			btnBrowse.setForeground(Color.BLACK);
			btnBrowse.setFont(new Font("MV Boli", Font.BOLD, 10));
			getContentPane().add(btnBrowse);

			
			
			JLabel lblKnownBooks = new JLabel("Select Known Books:");
			lblKnownBooks.setBounds(223, 172, 160, 17);
			lblKnownBooks.setForeground(new Color(139, 69, 19));
			lblKnownBooks.setFont(new Font("MV Boli", Font.BOLD, 14));
			getContentPane().add(lblKnownBooks);
			
			ImageIcon b10 = new ImageIcon("images\\add book to db.png");
			
			
			
			JButton btnAddBookTo = new JButton(b10);
			btnAddBookTo.setBounds(17, 252, 173, 23);
			btnAddBookTo.setForeground(Color.BLACK);
			btnAddBookTo.setFont(new Font("MV Boli", Font.BOLD, 10));
           btnAddBookTo.setBorder(BorderFactory.createEmptyBorder());
			
			btnAddBookTo.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {

			
					setVisible(false);
					AddBookUI BookUI=new AddBookUI(); 
					
				}
			});
			getContentPane().add(btnAddBookTo);
			
			JLabel lblNewLabel = new JLabel("AUTHOR PROFILES DETERMINATION");
			lblNewLabel.setBounds(158, 37, 761, 48);
			lblNewLabel.setForeground(new Color(139, 69, 19));
			lblNewLabel.setFont(new Font("MV Boli", Font.BOLD, 34));
			getContentPane().add(lblNewLabel);
			
			JLabel lblSubmitTheText = new JLabel("Submit the text and author:");
			lblSubmitTheText.setBounds(20, 109, 226, 21);
			lblSubmitTheText.setForeground(new Color(139, 69, 19));
			lblSubmitTheText.setFont(new Font("MV Boli", Font.BOLD, 14));
			getContentPane().add(lblSubmitTheText);
			
			textField_1 = new JTextField();
			textField_1.setBounds(17, 128, 187, 22);
			getContentPane().add(textField_1);
			textField_1.setColumns(10);
			textField_1.setBackground(Color.WHITE);
			
			ImageIcon b12 = new ImageIcon("images\\add author to db.png");
			
			
			JButton btnAddAuthorTo = new JButton(b12);
			btnAddAuthorTo.setBounds(17, 285, 173, 23);
			btnAddAuthorTo.setForeground(Color.BLACK);
			btnAddAuthorTo.setFont(new Font("MV Boli", Font.BOLD, 10));
btnAddAuthorTo.setBorder(BorderFactory.createEmptyBorder());
			
			btnAddAuthorTo.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {
					setVisible(false);
					try {
						AddAuthorUI AuthorUI=new AddAuthorUI();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			getContentPane().add(btnAddAuthorTo);
			
			

			ImageIcon b9 = new ImageIcon("images\\delete selected author.png");
		
			////
			
			JButton btnDeleteSelectedAuthor = new JButton(b9);
			btnDeleteSelectedAuthor.setBounds(17, 218, 173, 23);
			btnDeleteSelectedAuthor.setForeground(Color.BLACK);
			btnDeleteSelectedAuthor.setFont(new Font("MV Boli", Font.BOLD, 10));
btnDeleteSelectedAuthor.setBorder(BorderFactory.createEmptyBorder());
			
			btnDeleteSelectedAuthor.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {
                	//delete selected author
					int index=comboBox.getSelectedIndex();
					if(index==0||index==-1){
						infoBox("Warning: You have to select an author!", "TITLE BAR MESSAGE");
					}
					else{
						if (JOptionPane.showConfirmDialog(null, "Are you sure you want to delete "+ comboBox.getSelectedItem().toString()+" author from db?", "Request", 
							    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
							    == JOptionPane.YES_OPTION)
							{
							 //Do the request
							
							System.out.println("author id   "+authoridarry.get(index-1));
							System.out.println("index  "+index);
							//remove from author  and author book db
							if(AuthorManager.deleteAuthor(authoridarry.get(index-1))){
							infoBox("INFORMATION: Author removal completed successfully!", "TITLE BAR MESSAGE");
							authoridarry.remove(index-1);
							comboBox.removeItemAt(index);
							
							
							}
							}
						else{
							infoBox("Warning: Author removal completed unsuccessfully, Try again!", "TITLE BAR MESSAGE");
						}
					}
					
				}
			});
			getContentPane().add(btnDeleteSelectedAuthor);
			
			JLabel lblSelectedBooks = new JLabel("Selected Books:");
			lblSelectedBooks.setBounds(513, 172, 160, 17);
			lblSelectedBooks.setForeground(new Color(139, 69, 19));
			lblSelectedBooks.setFont(new Font("MV Boli", Font.BOLD, 14));
			getContentPane().add(lblSelectedBooks);

			
			JLabel label_1 = new JLabel("Algorithm settings:");
			label_1.setBounds(731, 109, 239, 21);
			label_1.setForeground(new Color(139, 69, 19));
			label_1.setFont(new Font("MV Boli", Font.BOLD, 17));
			getContentPane().add(label_1);
			
			JLabel label_2 = new JLabel("Number of groups:");
			label_2.setBounds(731, 128, 133, 22);
			label_2.setForeground(new Color(139, 69, 19));
			label_2.setFont(new Font("MV Boli", Font.BOLD, 12));
			getContentPane().add(label_2);
			
			JLabel lmodel_selection = new JLabel("Number of parts:");
			lmodel_selection.setBounds(731, 161, 124, 17);
			lmodel_selection.setForeground(new Color(139, 69, 19));
			lmodel_selection.setFont(new Font("MV Boli", Font.BOLD, 12));
			getContentPane().add(lmodel_selection);
			
			textField = new JTextField();
			textField.setBackground(Color.WHITE);
			textField.setBounds(857, 130, 86, 17);
			getContentPane().add(textField);
			textField.setColumns(10);
			
			textField_2 = new JTextField();
			textField_2.setBackground(Color.WHITE);
			textField_2.setBounds(857, 161, 86, 17);
			textField_2.setColumns(10);
			getContentPane().add(textField_2);
			
			ImageIcon b5 = new ImageIcon("images\\run nmf.png");
			button_2 = new JButton(b5);
			button_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					 if(textField_1.getText().isEmpty())
						{
							infoBox("No book selected!", "ERROR!");
							return;
						}
						if(textField.getText().isEmpty())
						{
							infoBox("No number of clusters selected!", "ERROR!");
							return;
						}
						if(textField_2.getText().isEmpty())
						{
							infoBox("No number of documents selected!", "ERROR!");
							return;
						}
						if(SelectedBookListArray.size()==0){
							infoBox("No known book selected!", "ERROR!");
							return;
						}
						
						NmfAlg.log="";
						ResultsUI.table=new JTable();
						
						button_3.setEnabled(true);
						pathes= new ArrayList<String>();
						for(int i=0;i<SelectedBookListArray.size();i++)
							pathes.add(SelectedBookListArray.get(i).getPath());
						pathes.add(textField_1.getText());
						//////////
						for(int k=0;k<RunningBooks.size();k++)
						
						System.out.println(RunningBooks.get(k));
						
						
						///////
						btnResults.setEnabled(false);
						RunningBooks=new ArrayList<Book>(SelectedBookListArray);
						Book b_temp=new Book();
						b_temp.setTitle("UnknownBook");
						b_temp.setPath(textField_1.getText());
						RunningBooks.add(b_temp);
						alg=1;
						//Algorithm.run(Integer.parseInt(textField.getText()),Integer.parseInt(textField_2.getText()),pathes);
						AlgorithmThread=new Thread(new Algorithm());
						AlgorithmThread.start();
					
				}
			});
			////
			button_2.setBorder(BorderFactory.createEmptyBorder());
			
			
			button_2.setBounds(738, 189, 101, 23);
			button_2.setForeground(Color.BLACK);
			button_2.setFont(new Font("MV Boli", Font.BOLD, 12));
			getContentPane().add(button_2);
			
button_3.setBorder(BorderFactory.createEmptyBorder());
			
			button_3.setBounds(738, 427, 86, 23);
			button_3.setForeground(Color.BLACK);
			button_3.setFont(new Font("MV Boli", Font.BOLD, 12));
			getContentPane().add(button_3);
			
			JLabel label_4 = new JLabel("Model Selection:");
			label_4.setBounds(731, 282, 145, 23);
			label_4.setForeground(new Color(139, 69, 19));
			label_4.setFont(new Font("MV Boli", Font.BOLD, 16));
			getContentPane().add(label_4);
			
			ImageIcon b6 = new ImageIcon("images\\run model selection.png");
			
			button_4 = new JButton(b6);
			button_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					if(txtpr.getText().isEmpty()){
						infoBox("No pracentege selected!", "ERROR!");
						return;
					}
					if(textField_1.getText().isEmpty())
					{
						infoBox("No book selected!", "ERROR!");
						return;
					}
					if(textField_2.getText().isEmpty())
					{
						infoBox("No number of documents selected!", "ERROR!");
						return;
					}
					if(SelectedBookListArray.size()==0){
						infoBox("No known book selected!", "ERROR!");
						return;
					}
					MainMenuUI.button_3.setEnabled(true);
					NmfAlg.log="";
					ResultsUI.table=new JTable();
					alg=2;
					pathes= new ArrayList<String>();
					
					for(int i=0;i<SelectedBookListArray.size();i++)
						pathes.add(SelectedBookListArray.get(i).getPath());
					pathes.add(textField_1.getText());
					
					RunningBooks=new ArrayList<Book>(SelectedBookListArray);
					Book b_temp=new Book();
					b_temp.setTitle("UnknownBook");
					b_temp.setPath(textField_1.getText());
					RunningBooks.add(b_temp);
					btnResults.setEnabled(false);
					//////////
					//for(int k=0;k<RunningBooks.size();k++)
					
					//System.out.println(RunningBooks.get(k));
					
					///////
					
					//Algorithm.run(Integer.parseInt(textField.getText()),Integer.parseInt(textField_2.getText()),pathes);
					AlgorithmThread=new Thread(new Algorithm());
					AlgorithmThread.start();
					
				}
			});
			button_4.setBorder(BorderFactory.createEmptyBorder());
			button_4.setBounds(738, 335, 168, 23);
			button_4.setForeground(Color.BLACK);
			button_4.setFont(new Font("MV Boli", Font.BOLD, 12));


			getContentPane().add(button_4);
			
			JScrollPane scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(513, 191, 188, 206);
			getContentPane().add(scrollPane_1);
			
			
			
			selectedBooks = new JList();
			scrollPane_1.setViewportView(selectedBooks);
			selectedBooks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			selectedBooks.setBorder(new EtchedBorder(EtchedBorder.RAISED, Color.BLACK, null));
			selectedBooks.setBackground(Color.WHITE);
			
			
			//fill book list by author
			listModel = new DefaultListModel();
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(211, 188, 188, 206);
			getContentPane().add(scrollPane);
			booklist = new JList(bookStringList(BookManager.getAllBooks()).toArray());
			scrollPane.setViewportView(booklist);
			booklist.setBackground(Color.WHITE);
			
			booklist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			booklist.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, null));
			
			//	fill author strings array, and author id array.
			FillAuthors();
			
			
			//
			comboBox = new JComboBox(strauthorArr);
			comboBox.setBackground(Color.WHITE);
			comboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent event) {
				    if( event.getStateChange() == ItemEvent.SELECTED ) {
				    	BookListArray= new ArrayList<Book>();

				        initializeBooklist();
				    }
				}
			});
					
			comboBox.setBounds(20, 188, 187, 23);
			getContentPane().add(comboBox);
			
				
			
			
			JLabel lblAuthors = new JLabel("Authors:");
			lblAuthors.setForeground(new Color(139, 69, 19));
			lblAuthors.setFont(new Font("MV Boli", Font.BOLD, 14));
			lblAuthors.setBounds(20, 172, 89, 17);
			getContentPane().add(lblAuthors);
			
			ImageIcon b3 = new ImageIcon("images\\gg.png");
			JButton removeButton = new JButton(b3);
			removeButton.setBorder(BorderFactory.createEmptyBorder());
			
			removeButton.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {
					  if(selectedBooks.getSelectedIndex()!=-1){
							removeItemFromSelectedBooksList(SelectedBookListArray.get(selectedBooks.getSelectedIndex()));
							}
					  
							
				  }
				});
			
			
			removeButton.setFont(new Font("MV Boli", Font.BOLD, 11));
			removeButton.setBounds(409, 314, 94, 17);
			getContentPane().add(removeButton);
			
			ImageIcon b2 = new ImageIcon("images\\ff.png");
			JButton btnAdd = new JButton(b2);
			////
			btnAdd.setBorder(BorderFactory.createEmptyBorder());
			
			btnAdd.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {

					  if(booklist.getSelectedIndex()!=-1){
							addItemToSelectedBooksList(BookListArray.get(booklist.getSelectedIndex()));
						}
							
				  }
				});
		
			btnAdd.setFont(new Font("MV Boli", Font.BOLD, 11));
			btnAdd.setBounds(409, 286, 94, 17);
			getContentPane().add(btnAdd);
			
			
			ImageIcon b8 = new ImageIcon("images\\delete selected books.png");
			
			JButton buttondeletebooks = new JButton(b8);
			buttondeletebooks.setForeground(Color.BLACK);
			buttondeletebooks.setFont(new Font("MV Boli", Font.BOLD, 10));
			buttondeletebooks.setBounds(513, 409, 188, 23);
			getContentPane().add(buttondeletebooks);
			////
			buttondeletebooks.setBorder(BorderFactory.createEmptyBorder());
			
			
			
			lblRunStatus = new JLabel("");
			lblRunStatus.setFont(new Font("MV Boli", Font.PLAIN, 10));
			lblRunStatus.setForeground(new Color(255, 0, 0));
			lblRunStatus.setBounds(731, 369, 314, 14);
			getContentPane().add(lblRunStatus);
			
			progressBar = new JProgressBar();
			progressBar.setForeground(new Color(210, 105, 30));
			progressBar.setBackground(Color.decode("#E7CCA6"));
			progressBar.setStringPainted(true);
			progressBar.setBounds(731, 402, 216, 14);
			getContentPane().add(progressBar);
			
			txtpr = new JTextField();
			txtpr.setBackground(Color.WHITE);
			txtpr.setColumns(10);
			txtpr.setBounds(857, 307, 86, 17);
			getContentPane().add(txtpr);
			
			JLabel lblPracentege = new JLabel("Percentage:");
			lblPracentege.setForeground(new Color(139, 69, 19));
			lblPracentege.setFont(new Font("MV Boli", Font.BOLD, 12));
			lblPracentege.setBounds(731, 306, 112, 18);
			getContentPane().add(lblPracentege);
			
			ImageIcon b16 = new ImageIcon("images\\results.png");
			
			
			
			btnResults = new JButton(b16);
			btnResults.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					Algorithm.result.setVisible(true);
					
				}
			});
			btnResults.setFont(new Font("MV Boli", Font.PLAIN, 11));
			btnResults.setEnabled(false);
            btnResults.setBorder(BorderFactory.createEmptyBorder());
			
			btnResults.setBounds(857, 427, 86, 23);
			getContentPane().add(btnResults);
			
			JMenuBar menuBar = new JMenuBar();
			menuBar.setForeground(new Color(244, 164, 96));
			menuBar.setFont(new Font("MV Boli", Font.PLAIN, 12));
			menuBar.setBounds(0, 0, 145, 34);
			getContentPane().add(menuBar);
			arg0_1 = new JMenu("!!!Information!!!");
			menuBar.add(arg0_1);
			arg0_1.setForeground(new Color(128, 0, 0));
			arg0_1.setFont(new Font("MV Boli", Font.BOLD, 15));
			
			//menu.addSeparator();
			JMenu submenu1 = new JMenu("Delete a book from database");
			submenu1.setFont(new Font("MV Boli", Font.PLAIN, 12));
			submenu1.setMnemonic(KeyEvent.VK_S);
			
						JMenuItem menuItem1 = new JMenuItem("If you want to delete \n a book from db choose one \n from \"Selected books:\" list");
						menuItem1.setFont(new Font("MV Boli", Font.PLAIN, 12));
						
						submenu1.add(menuItem1);
						
						arg0_1.add(submenu1);
						arg0_1.addSeparator();
						JMenu submenu2 = new JMenu("Delete an author from database");
						submenu2.setFont(new Font("MV Boli", Font.PLAIN, 12));
						

						JMenuItem menuItem2 = new JMenuItem("If you want to delete \n an author from db choose one \n from \"Authors:\" combo box");
						menuItem2.setFont(new Font("MV Boli", Font.PLAIN, 12));
						
						submenu2.add(menuItem2);
						
						arg0_1.add(submenu2);
						
						arg0_1.addSeparator();
						JMenu submenu3 = new JMenu("Add a new book to database");
						submenu3.setFont(new Font("MV Boli", Font.PLAIN, 12));
						submenu3.setMnemonic(KeyEvent.VK_S);
						
									JMenuItem menuItem3 = new JMenuItem("If you want to add \n a book to db press on \n\"Add book to db\" button");
									menuItem3.setFont(new Font("MV Boli", Font.PLAIN, 12));
									
										submenu3.add(menuItem3);
										
										arg0_1.add(submenu3);
										
										arg0_1.addSeparator();
										JMenu submenu4 = new JMenu("Add an author to database");
										submenu4.setFont(new Font("MV Boli", Font.PLAIN, 12));
										

										JMenuItem menuItem4 = new JMenuItem("If you want to add  an author to db press on  \"Add author to db\" button");
										menuItem4.setFont(new Font("MV Boli", Font.PLAIN, 12));
										
										submenu4.add(menuItem4);
										
										arg0_1.add(submenu4);
										
										arg0_1.addSeparator();
										JMenu submenu5 = new JMenu("NMF execution");
										submenu5.setFont(new Font("MV Boli", Font.PLAIN, 12));
										

										JMenuItem menuItem5= new JMenuItem("Upload unknown book by pressing \"Browse\" choose authors from combo box");
										menuItem5.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem6= new JMenuItem("Choose books from \"Select known book\" list and press \"Add\" button ");
										menuItem6.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem13= new JMenuItem("If u want to remove book from input stream press choose book from  \"Selected books\" list " );
										menuItem13.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem14= new JMenuItem("and press on\"Remove\" button ");
										menuItem14.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem7= new JMenuItem("Choose number of book parts, number of clusters and press on \"Run NMF\" button");
										menuItem7.setFont(new Font("MV Boli", Font.PLAIN, 12));
										
										submenu5.add(menuItem5);
										submenu5.add(menuItem6);
										submenu5.add(menuItem13);
										submenu5.add(menuItem14);
										submenu5.add(menuItem7);
										
										arg0_1.add(submenu5);
										
										arg0_1.addSeparator();
										JMenu submenu6 = new JMenu("Model selection execution");
										submenu6.setFont(new Font("MV Boli", Font.PLAIN, 12));
										

										JMenuItem menuItem8= new JMenuItem("Upload unknown book by pressing \"Browse\" choose authors from combo box");
										menuItem8.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem9= new JMenuItem("Choose books from \"Select known book\" list and press \"Add\" button ");
										menuItem9.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem11= new JMenuItem("If u want to remove book from input stream press choose book from  \"Selected books\" list " );
										menuItem11.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem12= new JMenuItem("and press on\"Remove\" button ");
										menuItem12.setFont(new Font("MV Boli", Font.PLAIN, 12));
										JMenuItem menuItem0= new JMenuItem("Choose number of book parts, precentege of corrupt data and press on \"Run Model Selection\" button");
										menuItem0.setFont(new Font("MV Boli", Font.PLAIN, 12));
										submenu6.add(menuItem8);
										submenu6.add(menuItem9);
										submenu6.add(menuItem11);
										submenu6.add(menuItem12);
										submenu6.add(menuItem0);
										
										arg0_1.add(submenu6);
										
										Component verticalStrut = Box.createVerticalStrut(20);
										verticalStrut.setBounds(731, 96, 1, 280);
										getContentPane().add(verticalStrut);
										
										Component verticalStrut_1 = Box.createVerticalStrut(20);
										verticalStrut_1.setBounds(731, 110, -7, 102);
										getContentPane().add(verticalStrut_1);
									


										ImagePanel panel = new ImagePanel(image);
										//JPanel panel_1= new JPanel();
										//JPanel panel= new JPanel();
										panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
										panel.setForeground(new Color(255, 69, 0));
										panel.setBounds(711, 96, 243, 365);
										getContentPane().add(panel);
										
										ImagePanel panel_1 = new ImagePanel(image);
										panel_1.setBounds(0, 96, 707, 365);
										panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
										getContentPane().add(panel_1);
										
										buttondeletebooks.addMouseListener(new MouseAdapter() {
											  @Override
											  public void mouseClicked(MouseEvent e) {

					if(SelectedBookListArray.isEmpty())
						infoBox("Warning: No books were selected! Please choose book from \n \"Selected Books\" list!", "TITLE BAR MESSAGE");
					else
						if (JOptionPane.showConfirmDialog(null, "Are you sure you want to delete books from data base?", "Request", 
							    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
							    == JOptionPane.YES_OPTION)
							{
							 //Do the request
							
							for(int i=0;i<SelectedBookListArray.size();i++)
								BookManager.deleteBook(SelectedBookListArray.get(i).getId());
							
							SelectedBookListArray.clear();
							initializeBooklist();
							selectedBooksListModel=new DefaultListModel(); 
							selectedBooks.setModel(selectedBooksListModel);
							
							
							infoBox("Selected books removed successfully!", "TITLE BAR MESSAGE");
							
							}
						else{
							infoBox("Warning: Book removal completed unsuccessfully, Try again!", "TITLE BAR MESSAGE");
						}
				}		
			});
				
		
		
			flag=-1;
			

			this.setVisible(true);
			
		
       }
		public ArrayList<Integer> getAuthoridarry() {
			return authoridarry;
		}
		public void setAuthoridarry(ArrayList<Integer> authoridarry) {
			this.authoridarry = authoridarry;
		}
		
		public JComboBox getComboBox() {
		return comboBox;
	}
	
		public void setComboBox(JComboBox comboBox) {
		this.comboBox = comboBox;
	}
		



	   public static void infoBox(String infoMessage, String location)
		{
		    JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + location, JOptionPane.INFORMATION_MESSAGE);
		}
	   
	  
	   /* initializes the book list at the main menu */
	   public void initializeBooklist()
	   {
		   FillAuthors();
	        if(comboBox.getSelectedIndex()==0){
	        	BookListArray=BookManager.getAllBooks();
	        }
	        else
	        {
	        	BookListArray=BookManager.getBookListByAuthor(arrAuthors.get(comboBox.getSelectedIndex()-1).getId());
	        }
		   ArrayList<String> strList=bookStringList(BookListArray);
		   DefaultListModel listModel= new DefaultListModel();
		   for(int i=0;i<strList.size();i++)
			   listModel.addElement(strList.get(i));
		   booklist.setModel(listModel);
	   }
	   
	   
	// add books to list and to selected books array!
	   public void addItemToSelectedBooksList(Book x){
		   boolean flag=false;
		  for(int i=0;i<SelectedBookListArray.size();i++)
		  {
		   if(SelectedBookListArray.get(i).getId()==x.getId())
		   	{
		   		flag=true;
		   	}
		  }
		  
		  if(flag==false){
		    SelectedBookListArray.add(x);
	   		String temp=x.getTitel();
	   		selectedBooksListModel.addElement(temp);
	   		selectedBooks.setModel(selectedBooksListModel);
	   		
		  }
	     
	   }
	   
	   
	   // removes books from list and from selected books array!
	   public void removeItemFromSelectedBooksList(Book x){
		   selectedBooksListModel.remove(selectedBooks.getSelectedIndex());
		   SelectedBookListArray.remove(x);
		 
	   }
	   
	   /* return Name+LastName instead of author */
	   public String authorToString(Author x)
	   {
		   String str = null;
		   str=x.getName()+" "+x.getLastName();
		   return str;
	   }
	   
	   /* get book array, return names string array */
	   public ArrayList<String> bookStringList(ArrayList<Book> x)
	   {
		   ArrayList<String> strList = new ArrayList<String>();
		   String str="aa";
		   for(int i=0;i<x.size();i++){
			   str=x.get(i).getTitel();
			   strList.add(str);
		   }
		   
		   return strList;
	   }
	   
	   public void FillAuthors()
	   {
		    arrAuthors=new ArrayList<Author>();			
			arrAuthors=AuthorManager.getAllAuthors();
			strauthorArr= new String[arrAuthors.size()+1];
			strauthorArr[0]="All Authors";
			authoridarry =new ArrayList<Integer>();
			for(int i=0;i<arrAuthors.size();i++){
				
				strauthorArr[i+1]=authorToString(arrAuthors.get(i));
				authoridarry.add(arrAuthors.get(i).getId());
			}
	   }
	   
	   public static void ChangeProgressBar()
	   {
   		/*	set progress bar indeterminate value	*/
   		if(MainMenuUI.progressBar.getValue()==0){
   			MainMenuUI.progressBar.setIndeterminate(true);
   			MainMenuUI.progressBar.setStringPainted(false);
   		}
   		else{
   			MainMenuUI.progressBar.setIndeterminate(false);
   			MainMenuUI.progressBar.setStringPainted(true);
   		}
   		/*	set progress bar indeterminate value	*/
	   }
		
		public static void main(String[] args) throws IOException{
			
			X=new MainMenuUI();

			

			
		}
	}


